﻿using System;
using System.Threading;
using Domain.Services;
using Observers;

namespace Domain
{
    internal class Program
    {
        private static readonly string[] customers = new[] { "Chris Missal", "Marla Singer", "Sammy Jankis", "Derek Vinyard", "Marvin Nash", "Trent Walker", "Dumpster Doggy", "Marsellus Wallace", "Mitch Martin", "Billy Costigan", "Peter Griffin", "Homer Simpson" };

        public static void Main(string[] args)
        {
            var service = new OrderService();

            //service.Attach(new LargeOrderObserver(1000m));       // Alerts when order amounts exceed threshold 'X'.
            //service.Attach(new OrderErrorObserver());            // Alerts when an order is in error.
            //service.Attach(new HighErrorCountObserver(2, 10));    // Alerts when fewer than 'X' orders are submitted in 'Y' minutes.

            //service.Attach(new OneTimeUseObserver());             // Drawback, the Observer cannot detach itself during notification; not without extra 
                                                                  // work anyway. These types of limitations are overcome by more complex observer patterns
                                                                  // like the transactional pub/sub framework that MassTransit is built on of.

            while (true)
            {
                var amount = GetRandomAmount();
                var customer = GetRandomCustomer();
                service.Submit(new Order { OrderAmount = amount, Customer = customer});
                Thread.Sleep(300);
                
                if(service.LastOrder.OrderNumber >= 1150) break;
            }
        }

        private static string GetRandomCustomer()
        {
            return customers[new Random().Next(0, customers.Length)];
        }

        private static decimal GetRandomAmount()
        {
            return (new Random().Next(5500, 108000) / 100m);
        }
    }
}